/**
 * 
 */
package com.client.model;

import java.io.Serializable;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.client.cache.PhoneNumberManager;
import com.util.Loggable;
import com.util.MobileRegion;
import com.util.MobileRegionUtil;
import com.util.ProvinceUtil;
import com.util.RegionUtil;
import com.utils.HttpUtils;

/**
 *
 */
public class User extends Loggable implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int id;
	private String guid;
	private String mobile;
	private String imsi;
	private String imei;
	private String ua;
	private String smsc;
	private int plugin;
	private int channelId;
	private int projectId;
	private int projectPkgId;
	private String mobileId;
	private String platform;		
	private int systemapp;			//是否可以切换网络
	private String province;		//省份
	private String region;			//地区
	private int networknumber;		//联网次数
	private int activationstatus;	//激活状态
	private Date regTime;			//注册时间
	private Date lastTime;			//最后访问时间
	private String carrier;			//运营商
	private String version;        //  SDK版本
	
	private int cpid;
	private String simsn; //sim卡号
	private String mac; //mac地址
	private String androidId;//				Androidid
	private String board;//				品牌
	private String manufacturer;//				厂商
	private String device;//				设备信息
	private String product;//				产品
	private String model;//				型号
	private String sdk;//				Android sdk版本
	private String sdkInt;//				Android sdk版本
	private String sdkrelease;//				发行信息
	private String appId;//应用ID
	private String chid;//CP自定义渠道
	
	public User() {
		
	}
	
	public User(int id, String guid, String mobile, String imsi, String imei,
			String smsc, String ua, int plugin, int channelId, int projectId, 
			String mobileId, String platform, int systemapp,String version,
			String androidId,
			String board,
			String manufacturer,
			String device,
			String product,
			String model,
			String sdk,
			String sdkInt,
			String sdkrelease,
			String simsn,
			String mac,
			int cpid,String appId,String chid) {
		super();
		this.id = id;
		this.guid = guid;
		this.mobile = mobile.replace("+86", "");
		this.imsi = imsi;
		this.imei = imei;
		this.ua = ua;
		if(smsc!=null)
			this.smsc = smsc.replace("+86", "");
		else
			this.smsc="";
		this.plugin = plugin;
		this.channelId = channelId;
		this.projectId = projectId;
		this.mobileId = mobileId;
		this.platform = platform;
		this.systemapp = systemapp;
		this.projectPkgId = -1;
		this.networknumber = 1;
		this.activationstatus = 0;
		this.regTime = new Date();
		this.lastTime = new Date();
		this.version = version;
		
		this.androidId = androidId;
		this.board = board;
		this.manufacturer = manufacturer;
		this.device  =device;
		this.product = product;
		this.model = model;
		this.sdk = sdk;
		this.sdkInt = sdkInt;
		this.sdkrelease = sdkrelease;
		this.simsn = simsn;
		this.mac =  mac;
		this.cpid = cpid;
		this.appId = appId;
		this.chid = chid;
		

		this.province = ProvinceUtil.UNKN;
		this.region = RegionUtil.UNKN;
		this.carrier = "UNKN";
		
		if (this.mobile != null && this.mobile.length() > 0)
		{
			MobileRegion mr = MobileRegionUtil.getInstance().findRegion(this.mobile);
			if (mr != null)
			{
				this.province = mr.getProvince();
				this.region = mr.getCity();
				this.carrier = mr.getType();
			}else
			{
				if (this.smsc != null && this.smsc.length() > 0)
				{
					String[] s = this.smsc.split(";");
					mr = MobileRegionUtil.getInstance().findRegion(s[s.length-1]);
					if (mr != null)
					{
						this.province = mr.getProvince();
						this.region = mr.getCity();
						this.carrier = mr.getType();
					}
				}
			}
		}
		else
		{
			if (this.smsc != null && this.smsc.length() > 0)
			{
				String[] s = this.smsc.split(";");
				MobileRegion mr = MobileRegionUtil.getInstance().findRegion(s[s.length-1]);
				if (mr != null)
				{
					this.province = mr.getProvince();
					this.region = mr.getCity();
					this.carrier = mr.getType();
				}
			}
		}
		//号段匹配有问题，
		if(this.province.equals(ProvinceUtil.UNKN))
		{
			String v = "";
			if (this.mobile != null && this.mobile.length() > 0)
			{
				v = this.mobile;
			}else if (this.smsc != null && this.smsc.length() > 0)
			{
				String[] s = this.smsc.split(";");
				v = s[s.length-1];
			}
			if(v.length()>7)
			{
				String ppc = PhoneNumberManager.getInstance().getPhoneNumberByPhonePrefix(v.substring(0,7));
				if(ppc!=null && ppc.length()>0)
				{
					String []s = ppc.split("_");
					if(s.length==3)
					{
						this.province =s[0];
						this.region = s[2];
						this.carrier = s[1];
					}
				}
			}
		}
		//取得不到省份，根据imsi设置运营商信息。
		if(this.carrier==null || this.carrier.length()<=0||carrier.equals("UNKN"))
		{
			this.carrier = getUserCarrier(this.imsi);
		}
		//取不到省份，移动根据simsn设置省份
		if(this.province.equals(ProvinceUtil.UNKN))
		{
			if(this.carrier.indexOf("移动")>-1)
			{
				this.province = getUserProvinceBySimsn(this.simsn);
			}
		}
	}
	public String getUserProvinceBySimsn(String simsn)
	{
//		01：北京 02：天津 03：河北 04：山西 05：内蒙古 06：辽宁 07：吉林 08：黑龙江
//		　　09：上海 l0：江苏 11：浙江 12：安徽 13：福建 14：江西 15：山东 16：河南
//		　　17：湖北 18：湖南 19：广东 20：广西 21：海南 22：四川 23：贵州 24：云南
//		　　25：西藏 26：陕西 27：甘肃 28：青海 29：宁夏 30：新疆 31：重庆
		Map<String,String> map = new HashMap<String,String>();
		map.put("01", "北京");
		map.put("02", "天津");
		map.put("03", "河北");
		map.put("04", "山西");
		map.put("05", "内蒙古");
		map.put("06", "辽宁");
		map.put("07", "吉林");
		map.put("08", "黑龙江");
		map.put("09", "上海");
		map.put("10", "江苏");
		map.put("11", "浙江");
		map.put("12", "安徽");
		map.put("13", "福建");
		map.put("14", "江西");
		map.put("15", "山东");
		map.put("16", "河南");
		map.put("17", "湖北");
		map.put("18", "湖南");
		map.put("19", "广东");
		map.put("20", "广西");
		map.put("21", "海南");
		map.put("22", "四川");
		map.put("23", "贵州");
		map.put("24", "云南");
		map.put("25", "西藏");
		map.put("26", "陕西");
		map.put("27", "甘肃");
		map.put("28", "青海");
		map.put("29", "宁夏");
		map.put("30", "新疆");
		map.put("31", "重庆");
		
		
		String p = ProvinceUtil.UNKN;
		if(simsn!=null && simsn.length()>10)
		{
			String pn = simsn.substring(8,10);
			p = map.get(pn);
		}
		if(p==null || p.length()<=0)
		{
			p = ProvinceUtil.UNKN;
		}
		return p;
	}
	public String getUserCarrier(String imsi1)
	{
		String yys = "UNKN";
		if(imsi1!=null && imsi1.length()>0)
		{
			 if (imsi1.startsWith("46000") || imsi1.startsWith("46002") || imsi1.startsWith("46007")) {
				 yys = "移动";
	         } else if (imsi1.startsWith("46001")) {
	        	 yys = "联通";
	         } else if (imsi1.startsWith("46003")) {
	        	 yys = "电信";
	         } else {
	             //默认是中国移动
	        	 yys = "移动";
	         }
		}else {
            //默认是中国移动
       	 yys = "移动";
        }
		return yys;
	}
	
	
	
	public String getAppId() {
		return appId;
	}

	public void setAppId(String appId) {
		this.appId = appId;
	}

	public String getChid() {
		return chid;
	}

	public void setChid(String chid) {
		this.chid = chid;
	}

	public int getCpid() {
		return cpid;
	}

	public void setCpid(int cpid) {
		this.cpid = cpid;
	}

	public String getSimsn() {
		return simsn;
	}

	public void setSimsn(String simsn) {
		this.simsn = simsn;
	}

	public String getMac() {
		return mac;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public String getAndroidId() {
		return androidId;
	}

	public void setAndroidId(String androidId) {
		this.androidId = androidId;
	}

	public String getBoard() {
		return board;
	}

	public void setBoard(String board) {
		this.board = board;
	}

	public String getManufacturer() {
		return manufacturer;
	}

	public void setManufacturer(String manufacturer) {
		this.manufacturer = manufacturer;
	}

	public String getDevice() {
		return device;
	}

	public void setDevice(String device) {
		this.device = device;
	}

	public String getProduct() {
		return product;
	}

	public void setProduct(String product) {
		this.product = product;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public String getSdk() {
		return sdk;
	}

	public void setSdk(String sdk) {
		this.sdk = sdk;
	}

	public String getSdkInt() {
		return sdkInt;
	}

	public void setSdkInt(String sdkInt) {
		this.sdkInt = sdkInt;
	}

	

	public String getSdkrelease() {
		return sdkrelease;
	}

	public void setSdkrelease(String sdkrelease) {
		this.sdkrelease = sdkrelease;
	}

	/**
	 * @return the version
	 */
	public String getVersion() {
		return version;
	}

	/**
	 * @param version the version to set
	 */
	public void setVersion(String version) {
		this.version = version;
	}

	public String getCarrier() {
		return carrier;
	}

	public void setCarrier(String carrier) {
		this.carrier = carrier;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the guid
	 */
	public String getGuid() {
		return guid;
	}
	/**
	 * @param guid the guid to set
	 */
	public void setGuid(String guid) {
		this.guid = guid;
	}
	/**
	 * @return the mobile
	 */
	public String getMobile() {
		return mobile;
	}
	/**
	 * @param mobile the mobile to set
	 */
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	/**
	 * @return the imsi
	 */
	public String getImsi() {
		return imsi;
	}
	/**
	 * @param imsi the imsi to set
	 */
	public void setImsi(String imsi) {
		this.imsi = imsi;
	}
	/**
	 * @return the imei
	 */
	public String getImei() {
		return imei;
	}
	/**
	 * @param imei the imei to set
	 */
	public void setImei(String imei) {
		this.imei = imei;
	}
	/**
	 * @return the ua
	 */
	public String getUa() {
		return ua;
	}
	/**
	 * @param ua the ua to set
	 */
	public void setUa(String ua) {
		this.ua = ua;
	}
	/**
	 * @return the smsc
	 */
	public String getSmsc() {
		return smsc;
	}
	/**
	 * @param smsc the smsc to set
	 */
	public void setSmsc(String smsc) {
		this.smsc = smsc;
	}
	/**
	 * @return the channelId
	 */
	public int getChannelId() {
		return channelId;
	}
	/**
	 * @param channelId the channelId to set
	 */
	public void setChannelId(int channelId) {
		this.channelId = channelId;
	}
	/**
	 * @return the projectId
	 */
	public int getProjectId() {
		return projectId;
	}
	/**
	 * @param projectId the projectId to set
	 */
	public void setProjectId(int projectId) {
		this.projectId = projectId;
	}
	
	public void setRegTime(String regTime)
	{
		if (regTime == null || regTime.length() == 0)
		{
			this.regTime = new Date();
			return;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try
		{
			this.regTime = sdf.parse(regTime);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * @return the mobileId
	 */
	public String getMobileId() {
		return mobileId;
	}

	/**
	 * @param mobileId the mobileId to set
	 */
	public void setMobileId(String mobileId) {
		this.mobileId = mobileId;
	}

	/**
	 * @return the projectPkgId
	 */
	public int getProjectPkgId() {
		return projectPkgId;
	}

	/**
	 * @param projectPkgId the projectPkgId to set
	 */
	public void setProjectPkgId(int projectPkgId) {
		this.projectPkgId = projectPkgId;
	}

	/**
	 * @return the regTime
	 */
	public Date getRegTime() {
		return regTime;
	}

	/**
	 * @param regTime the regTime to set
	 */
	public void setRegTime(Date regTime) {
		this.regTime = regTime;
	}

	/**
	 * @return the lastTime
	 */
	public Date getLastTime() {
		return lastTime;
	}

	/**
	 * @param lastTime the lastTime to set
	 */
	public void setLastTime(Date lastTime) {
		this.lastTime = lastTime;
	}

	/**
	 * @return the province
	 */
	public String getProvince() {
		return province;
	}

	/**
	 * @param province the province to set
	 */
	public void setProvince(String province) {
		this.province = province;
	}

	/**
	 * @return the region
	 */
	public String getRegion() {
		return region;
	}

	/**
	 * @param region the region to set
	 */
	public void setRegion(String region) {
		this.region = region;
	}

	/**
	 * @return the platform
	 */
	public String getPlatform() {
		return platform;
	}

	/**
	 * @param platform the platform to set
	 */
	public void setPlatform(String platform) {
		this.platform = platform;
	}

	/**
	 * @return the systemapp
	 */
	public int getSystemapp() {
		return systemapp;
	}

	/**
	 * @param systemapp the systemapp to set
	 */
	public void setSystemapp(int systemapp) {
		this.systemapp = systemapp;
	}

	/**
	 * @return the plugin
	 */
	public int getPlugin() {
		return plugin;
	}

	/**
	 * @param plugin the plugin to set
	 */
	public void setPlugin(int plugin) {
		this.plugin = plugin;
	}

	public int getNetworknumber() {
		return networknumber;
	}

	public void setNetworknumber(int networknumber) {
		this.networknumber = networknumber;
	}

	public int getActivationstatus() {
		return activationstatus;
	}

	public void setActivationstatus(int activationstatus) {
		this.activationstatus = activationstatus;
	}
	
	
}
